import { Body, ClassSerializerInterceptor, Controller, Delete, Get, HttpException, HttpStatus, Inject, LoggerService, Param, ParseIntPipe, Post, Put, SerializeOptions, UseInterceptors } from '@nestjs/common';
import { ApiBearerAuth, ApiBody, ApiOperation, ApiParam, ApiResponse, ApiTags } from '@nestjs/swagger';
import { WINSTON_MODULE_NEST_PROVIDER } from 'nest-winston';
import { CreateUserDto, UpdateUserDto } from 'src/shared/dto/user.dto';
import { User } from 'src/shared/entities/user.entity';
import { UserService } from 'src/shared/services/user.service';
import { Result } from 'src/shared/vo/reuslt';

@Controller('api/users')
@ApiTags('api/users')
// @SerializeOptions({strategy: 'excludeAll'}) // SerializeOptions配合UseInterceptors一起使用
@UseInterceptors(ClassSerializerInterceptor) // 加上这个装饰器才可以使用@Exclude@@Transformd等排除转换保密信息，比如密码，手机号
export class UserController {
    // @Inject(WINSTON_MODULE_NEST_PROVIDER) private readonly winstonLogger: LoggerService
    constructor(private readonly userService: UserService){}
    @Get()
    @ApiOperation({summary: '获取所有用户列表'})
    @ApiResponse({status: 200, description: '成功返回用户列表',type:[User]})
    async findAll(){
        // this.winstonLogger.error('winstonLogger 日记')
        return this.userService.findAll()
    }
    @Get(':id')
    @ApiOperation({summary: '根据Id获取某个用户信息'})
    @ApiParam({name: 'id', description: '用户Id', type:Number})
    @ApiResponse({status: 200, description: '成功返回用户列表',type:User})
    @ApiResponse({status: 404, description: '用户未找到'})
    async findOne(@Param('id', ParseIntPipe) id:number){
        const result = await this.userService.findOne({
            where: {id}
        })
        if (result) {
            return result
        } else {
            throw new HttpException('用户未找到', HttpStatus.NOT_FOUND)
        }
        
    }
    @Post()
    @ApiOperation({summary: '创建用户'})
    @ApiBearerAuth()
    @ApiBody({type: CreateUserDto})
    @ApiResponse({status: 201, description: '用户创建成功',type:User})
    @ApiResponse({status: 400, description: '请求参数错误'})
    async create(@Body() createUserDto: CreateUserDto){
        console.log('createUserDto', createUserDto)
        return this.userService.create(createUserDto)
    }
    @Put(':id')
    @ApiOperation({summary: '更新用户'})
    @ApiBody({type: UpdateUserDto})
    @ApiResponse({status: 201, description: '用户更新成功',type:Result})
    @ApiResponse({status: 400, description: '请求参数错误'})
    @ApiResponse({status: 404, description: '用户未找到'})
    async update(@Param('id', ParseIntPipe) id:number, @Body() updateUserDto: UpdateUserDto){
        const result = await this.userService.update(id, updateUserDto)
        if (result.affected){
            return Result.success('更新用户成功')
        } else {
            throw new HttpException('用户未找到', HttpStatus.NOT_FOUND)
        }
    }

    @Delete(':id')
    @ApiOperation({summary: '删除用户'})
    @ApiParam({name: 'id', description: '用户Id', type:Number})
    @ApiResponse({status: 200, description: '删除用户成功',type:Result})
    @ApiResponse({status: 404, description: '用户未找到'})
    async delete(@Param('id', ParseIntPipe) id:number){
        const result = await this.userService.delete(id)
        if (result.affected){
            return Result.success('删除用户成功')
        } else {
            throw new HttpException('用户未找到', HttpStatus.NOT_FOUND)
        }
    }
    
}
